<?php
class Order_Model_OrderPickupMap
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Order_Model_DbTable_OrderPickupMap();
	}

	/**
	*Create A Record  
	*@param rowSet 
	*@return boolean
	*/
	public  function create($row){
 		return $this->_table->insert($row); 
	}

	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $order_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto("opm_id= ?", $id);
		return $this->_table->update($row, $where);
	}
	
	/**
	*Delete One Row
	*@param int $order_id 
	*@return boolean
	*/
	public function delete($id){
		$where = $this->_table->getAdapter()->quoteInto("opm_id= ?", $id);

		return $this->_table->delete($where);
	}
	/**
	*Delete some Rows
	*@param string $order_code 
	*@return int
	*/
	public function deleteByOrderCode($order_code){
		$where = $this->_table->getAdapter()->quoteInto("orders_code= ?", $order_code);
		return $this->_table->delete($where);
	}


	/**
	*Get Rowset  
	*@param string $order_code
	*@return Rowset
	*/
	public function getByOrdersCode($orders_code){
		$where = $this->_table->getAdapter()->quoteInto('orders_code= ?', $orders_code);
		$result = $this->_table->fetchRow($where);
		
		if($result instanceof Zend_Db_Table_Row){
			return $result->toArray();										
		}
		return $result;
	}
	
	public function getByfield($field=1,$value=0){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto("opm_id = ?",$id);	
		return $this->_table->fetchRow($where);
	}
	
	public function getByOpCode($opCode){
		$where = $this->_table->getAdapter()->quoteInto("op_code = ?",$opCode);	
		$result =  $this->_table->fetchAll($where);
		if($result instanceof Zend_Db_Table_RowSet){
			return $result->toArray();										
		}
		return $result;
	}
	
	public function getAllByCond($condition=array(),$type='*',$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["op_code"]) && $condition["op_code"]!=''){
            $select->where(" op_code = ?",$condition["op_code"]);
        }
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchAll($sql);
	}
	
	/**
	 * 根据条件返回一条数据
	 */
	public function getRowByCond($condition=array(),$type='*',$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["orders_code"]) && $condition["orders_code"]!=''){
            $select->where(" orders_code = ?",$condition["orders_code"]);
        }
		if(isset($condition["op_code"]) && $condition["op_code"]!=''){
            $select->where(" op_code = ?",$condition["op_code"]);
        }
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql);
	}
	
	/**
	 * 根据条件更新一条数据
	 */
	public function updateByCond($row,$cond)
	{ 
		$adapter = $this->_table->getAdapter();
		$where = $adapter->quoteInto(" op_code= ?", $cond['op_code']);
		$where .= $adapter->quoteInto(" and orders_code= ?", $cond['orders_code']);
		return $this->_table->update($row, $where);
	}
	
	
}